$(function () { //匹配以及聊天

    $("input").blur(function () {  //解决键盘弹起,将页面顶起的问题
        $("html,body").animate({scrollTop: document.documentElement.clientHeight}, 500);
    });

    $(".animate ").fadeIn(3000);

    let sexIndex = 3;
    $(".sex-box .layui-inline").click(function () { //选择性别点击
        sexIndex = $(this).index();//性别 0男1女

        $(this).addClass(" sex-box-style");
        $(this).siblings().removeClass("sex-box-style");
        $(".username").addClass("layui-anim layui-anim-scaleSpring").show();
        ;
    })

    $(".intoChat button").click(function () {   //进入聊天室点击

        thisButton = $(this);

        if (sexIndex == 3) {
            layer.msg("您貌似忘了选择性别了呢~");
            return false;
        }

        let username = $(".username input").val();

        if (username.length == 0) {
            layer.msg("给自己取个昵称吧~^_^");
            return false;
        }



        $(this).hide(); //隐藏按钮
        $(this).siblings().show();  //loading

        let cut_num = 30;
        $(".cut-num").text(cut_num);
        cut_num_func = setInterval(function () {

            if (cut_num == 0) {
                $(".cut-num").text("");
                clearInterval(cut_num_func);        //解决避免匹配时间为负数的尴尬
            } else {
                $(".cut-num").text(cut_num);
                cut_num--;
            }


        }, 1000);


        if(wxAddress){  //判断是微信用户,并且还获取了对方的经纬度
            let latitude=$(".location .latitude").val();
            let longitude=$(".location .longitude").val();
            let address=$(".location-address").text();
            let chatAddress=$(".city-district").val();
             user = {  //用户信息
                username: username,
                client_id: client_id,
                sex: sexIndex,
                longitude:longitude,
                latitude:latitude,
                wxAddress:wxAddress,
                address:address,
                 chatAddress:chatAddress
            };
        }else{
             user = {  //用户信息
                username: username,
                client_id: client_id,
                sex: sexIndex,

            };
        }


        $.post('/bind', user, function (data) {

            let code = JSON.parse(data);
            if (code.code == 404) {
                clearInterval(cut_num_func);
                thisButton.show();
                thisButton.siblings().hide();
                layer.msg(code.message);
            }

            if (code.code == 200) {
                $(".loding").hide();
                $(".layui-progress").show();
                let progress = 10;
                layer.msg("匹配成功~正在载入房间~");
                setProgress = setInterval(function () {
                    element.progress('demo', `${progress}%`)
                    progress += 5;
                    if (progress >= 106) {
                        clearInterval(setProgress);

                        $.ajax({
                            type: "get",
                            url: "/room",
                            data: {
                                client_id: code.client_id,
                                user_name: code.user_name,
                                sex: code.sex,
                                room_id: code.room_id
                            },
                            success: function (data) {
                                $("body").html(data);
                            }

                        })
                    }

                }, 100)
            }
        }, 'json');
    })

})